package com.dc.controller;

import com.alibaba.fastjson.JSON;
import com.dc.bean.model.BaseModel;
import com.dc.service.NoticeService;
import com.dc.service.impl.NoticeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;

@WebServlet(name="NoticeServlet",urlPatterns = {"/selectNotice.do","/delectNotice.do","/addNotice.do"})
public class NoticeController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = req.getRequestURI();
        url = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
        System.out.println(url);
        Method method=null;
        try {
            method=getClass().getDeclaredMethod(url,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,req,resp);
        }catch (Exception e){
            System.out.println("java反射失败");
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
//    查询所有公告
    public void selectNotice(HttpServletRequest req,HttpServletResponse resp){
        try {
            NoticeService noticeService = new NoticeServiceImpl();
            BaseModel baseModel = new BaseModel();
            baseModel = noticeService.selectNotice(null);
            String baseModelStr = JSON.toJSONString(baseModel);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();
        }catch (Exception e){
            System.out.println("查询失败");
            e.printStackTrace();
        }
    }
//    添加公告
    public void  addNotice(HttpServletRequest req,HttpServletResponse resp){
            try {
                req.setCharacterEncoding("utf-8");
                NoticeService noticeService = new NoticeServiceImpl();
                Object param[] = new Object[3];
                param[0] = req.getParameter("Title");
                param[1] = req.getParameter("Content");
                param[2] = new Date();
                BaseModel baseModel = new BaseModel();
                baseModel = noticeService.addNotice(param);
                String baseModelStr = JSON.toJSONString(baseModel);
                resp.setContentType("application/json;charset=utf-8");
                resp.setCharacterEncoding("utf-8");
                resp.getWriter().write(baseModelStr);
                resp.getWriter().flush();
                resp.getWriter().close();
            }catch (Exception e){
                System.out.println("新增公告失败");
                e.printStackTrace();
            }
    }
//    删除公告
    public void delectNotice(HttpServletRequest req,HttpServletResponse resp){
        try {
            req.setCharacterEncoding("utf-8");
            String ids = req.getParameter("ids");
            NoticeService noticeService = new NoticeServiceImpl();
            BaseModel baseModel = new BaseModel();
            baseModel = noticeService.delectNotice(ids);
            String baseModelStr = JSON.toJSONString(baseModel);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();
        }catch (Exception e){
            System.out.println("删除失败");
            e.printStackTrace();
        }
    }
}
